import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lib_core/customization/flutter/cupertino/text_selection.dart';
import 'package:lib_uikit/dimen/gt_dimensions_v3.dart';
import 'package:lib_uikit/lib_uikit.dart';

enum GTSearchTypeV3 { default_type, dropdown }

enum GTSearchSizeV3 { size40, size30 }

class GTSearchViewV3 extends StatefulWidget {
  final String? hint;
  final ValueChanged<String>? onSubmit;
  final ValueChanged<String>? onTextChanged;
  final String? text;
  final String? rightText;
  final VoidCallback? onRightPressed;
  final Widget? rightIcon;
  final GTSearchTypeV3? type;
  final Color? rightColor;
  final bool? autoFocus;
  final FocusNode? focusNode;
  final TextEditingController? textEditingController;
  final GTSearchSizeV3? size;
  final TextInputType? inputType;
  final List<TextInputFormatter>? inputFormatter;
  final bool? showScan;
  final VoidCallback? onScanPressed;

  const GTSearchViewV3(
      {super.key,
      this.hint,
      this.onSubmit,
      this.onTextChanged,
      this.text,
      this.rightText,
      this.onRightPressed,
      this.rightIcon,
      this.type = GTSearchTypeV3.default_type,
      this.rightColor,
      this.autoFocus,
      this.focusNode,
      this.textEditingController,
      this.size,
      this.inputType = TextInputType.text,
      this.inputFormatter,
      this.showScan,
      this.onScanPressed});

  @override
  State<StatefulWidget> createState() => _SearchStateV3();
}

class _SearchStateV3 extends State<GTSearchViewV3> {
  Color strokeColor = GTColorsV3.divider5.color;
  bool hasFocus = false;
  bool isShowDelete = false;
  FocusNode _focusNode = FocusNode();
  TextEditingController _textEditingController = TextEditingController();
  double _height = 40;
  double _textSize = 14;
  double _iconSize = 14;
  bool _showScan = false;

  @override
  void initState() {
    super.initState();
    _textEditingController = widget.textEditingController ?? TextEditingController();
    _textEditingController.text = widget.text ?? "";
    _showScan = widget.showScan ?? false;
    _focusNode = widget.focusNode ?? FocusNode();
    _focusNode.addListener(_onFocusChange);
    if ((widget.size ?? GTSearchSizeV3.size40) == GTSearchSizeV3.size30) {
      _height = 30;
      _textSize = 13;
      _iconSize = 14;
    }
  }

  void _onFocusChange() {
    setState(() {
      //3.1样式改为完成态去掉蓝色边框
      hasFocus = _focusNode.hasFocus;
    });
  }

  @override
  void dispose() {
    // 释放资源
    _focusNode.dispose();
    _textEditingController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    isShowDelete = _textEditingController.text.isNotEmpty;

    return Row(
      children: <Widget>[
        Expanded(
          child: Container(
            alignment: Alignment.center,
            height: _height,
            decoration: BoxDecoration(
              color: GTColorsV3.bg1.color,
              border: Border.all(
                color: hasFocus ? GTColorsV3.text9.color : GTColorsV3.divider5.color,
                width: GTDimensionsV3.strokeWidth,
              ),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                const SizedBox(
                  width: 12,
                ),
                Icon(
                  GTIconFont.e8a3,
                  size: 16,
                  color: GTColorsV3.icon1.color,
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: Align(
                    alignment: Alignment.center,
                    child: TextField(
                      autofocus: widget.autoFocus ?? false,
                      selectionControls: gtCupertinoTextSelectionControls,
                      controller: _textEditingController,
                      focusNode: _focusNode,
                      cursorColor: GTColorsV3.icon5.color,
                      cursorWidth: 2,
                      cursorRadius: const Radius.circular(2),
                      keyboardType: widget.inputType,
                      inputFormatters: widget.inputFormatter,
                      textAlignVertical: TextAlignVertical.center,
                      textInputAction: TextInputAction.search,
                      onSubmitted: (String value) {
                        if (value.isNotEmpty) {
                          widget.onSubmit?.call(value);
                        } else {
                          //恢复默认态
                          setState(() {
                            hasFocus = false;
                          });
                        }
                      },
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: widget.hint,
                        isCollapsed: true,
                        hintStyle: gtTextStyleW500(
                          fontSize: _textSize,
                          color: GTColorsV3.text3.color,
                        ),
                        suffixIconConstraints: const BoxConstraints(),
                        suffixIcon: isShowDelete
                            ? GestureDetector(
                                onTap: () {
                                  _textEditingController.clear();
                                  setState(() {
                                    isShowDelete = false;
                                    //点击删除icon把onTextChanged回调返回的text置空
                                    widget.onTextChanged?.call("");
                                  });
                                },
                                child: Padding(
                                  padding: EdgeInsets.symmetric(horizontal: _showScan ? 8 : 12, vertical: 6),
                                  child: Icon(
                                    GTIconFont.e807,
                                    size: _iconSize,
                                    color: GTColorsV3.icon3.color,
                                  ),
                                ),
                              )
                            : null,
                      ),
                      onChanged: (text) {
                        setState(() {
                          widget.onTextChanged?.call(text);
                          isShowDelete = text.isNotEmpty;
                        });
                      },
                      style: gtTextStyleW500(
                        fontSize: _textSize,
                        color: GTColorsV3.text1.color,
                      ),
                    ),
                  ),
                ),
                if (_showScan) ...[
                  GestureDetector(
                    onTap: widget.onScanPressed,
                    child: Row(
                      children: [
                        Container(
                          width: 1,
                          height: 24,
                          color: GTColorsV3.divider5.color,
                        ),
                        const SizedBox(width: 8),
                        Icon(
                          GTIconFont.e8ec,
                          size: 16,
                          color: GTColorsV3.icon1.color,
                        ),
                        const SizedBox(width: 12),
                      ],
                    ),
                  ),
                ],
              ],
            ),
          ),
        ),
        if (widget.rightText != null) ...[
          GestureDetector(
              onTap: widget.onRightPressed,
              child: Padding(
                padding: const EdgeInsetsDirectional.fromSTEB(12, 6, 0, 6),
                child: Text(
                  widget.rightText ?? "",
                  style: gtTextStyleW400(
                    fontSize: 14,
                    color: widget.rightColor ?? GTColorsV3.text2.color,
                  ),
                ),
              )),
        ],
        if (widget.rightIcon != null)
          GestureDetector(
              onTap: widget.onRightPressed,
              child: Padding(
                padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 0, 0),
                child: widget.rightIcon,
              )),
      ],
    );
  }
}
